package com.bdqn.ch02.SL;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * XMLText  示例2 创建收藏信息
 *
 * @aurhor Administrator  whs
 * @since 2024/6/27
 */
public class XMLText {

    public void getDocument(){
        Document doc = null;
        // 获取DOM树

        //1.得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //2.从DOM工厂获得 DOM 解析器
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();

            //3.解析 XML 文档 得到一个Document 即 DOM 树
            doc = (Document) db.parse("D:\\Frame\\frames\\web\\collectMessage.xml");

            //4.读取新闻
            NodeList list = doc.getElementsByTagName("pubDate");
            Element pubDateElement = (Element) list.item(0);

            //5.读取文本节点
            String pubDate = pubDateElement.getFirstChild().getNodeValue();  // 获取第一个子节点的值
            System.out.println(pubDate);

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        XMLText text = new XMLText();
        text.getDocument();
    }


}
